package ourfilter;

import java.awt.image.renderable.ParameterBlock;

import javax.media.jai.JAI;
import javax.media.jai.operator.MedianFilterDescriptor;

import ourImage.OurPlanarImage;
import pimpmypipe.filter.DataTransformationFilter;
import pimpmypipe.interfaces.Writeable;
import pimpmypipe.interfaces.Readable;
import pimpmypipe.pipe.Pipe;

/**
 * 
 * @author Marcel Meraner
 */
public class MedianFilter extends DataTransformationFilter<OurPlanarImage> {
    
    private static final int DEFAULT_MATRIX = 9;
    private int _matrixSize = DEFAULT_MATRIX;
    private boolean _filterEnabled = false;
    
    
    public MedianFilter(){
        super(new Pipe<OurPlanarImage>(), new Pipe<OurPlanarImage>());
    }
    public MedianFilter(Readable<OurPlanarImage> in, Writeable<OurPlanarImage> out){
        super(in, out);
    }

    public MedianFilter(Pipe<OurPlanarImage> in, Pipe<OurPlanarImage> out){
        super(in, out);
        in.setOut(this);
        out.setIn(this);
    }
    
    @Override
    protected void process(OurPlanarImage entity) {
        if (_filterEnabled) {
            ParameterBlock pb = new ParameterBlock();
            pb.addSource(entity.getImage());
            pb.add(MedianFilterDescriptor.MEDIAN_MASK_SQUARE);
            pb.add(_matrixSize);
            entity.setImage(JAI.create("medianfilter", pb, null));
        }
    }

    public boolean isEnabled() {
        return _filterEnabled;
    }

    public void setFilterEnabled(boolean enabled) {
        _filterEnabled = enabled;
    }
    
    public void setMatrixSize(int size){
        _matrixSize = size;
    }
    
}
